package com.cac.demo.service.mswptoolbasic;

import com.cac.demo.dao.mswptoolbasic.MsWpToolBasicDao;
import com.cac.demo.entity.msWpTool.MsWpToolBasicDTO;
import com.cac.demo.util.DateUtil;
import com.cac.demo.util.adapter.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MsWpToolBasicService {

    @Autowired
    private MsWpToolBasicDao msWpToolBasicDao;



    /**
     * 按照条件分页查询
     * @param queryReqBean
     * @param orderBy
     * @return
     */
    @Transactional
    public QueryRespBean<MsWpToolBasicDTO> searchMsWpToolBasicByPage(QueryReqBean<MsWpToolBasicDTO> queryReqBean, String orderBy, String Keywords){
        QueryRespBean<MsWpToolBasicDTO> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsWpToolBasicDTO searchParams = queryReqBean.getSearchParams();
            Page<MsWpToolBasicDTO> dataList = msWpToolBasicDao.searchMsWpToolBasicByPage(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }

    @Transactional
    public List<MsWpToolBasicDTO> searchMsWpToolBasicByPage(MsWpToolBasicDTO searchParams, String orderBy, String Keywords){
        try{
            List<MsWpToolBasicDTO> dataList = msWpToolBasicDao.searchMsWpToolBasicByPage(searchParams,orderBy,Keywords);
            return dataList;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 新增单个数据
     * @param
     * @return
     */
    @Transactional()
    public int insertMsWpToolBasic(Map map){
        try{
            if(!Objects.isNull(map.get("toolCode"))&&(!"".equals(map.get("toolCode")))){
                int count = msWpToolBasicDao.insertMsWpToolBasicDTO(map, ComUtil.getId(), DateUtil.nowTime());
                return count;
            }else {
                return 0;
            }

        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    //查询工具包器具编码
    @Transactional
    public int queryToolCode(String carCode) {
        try{
            int count = msWpToolBasicDao.queryToolCode(carCode);
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 批量修改数据
     * @param
     * @return
     */
    @Transactional
    public int updateMsWpToolMany(Map map){
        try{
            int count = msWpToolBasicDao.updateMsWpToolDTO(map,DateUtil.nowTime());
            if(count==0){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }

    /**
     * 调用封装方法，将新数据copy到旧数据中
     * @param msWpToolBasicDTO
     * @return
     */
    private MsWpToolBasicDTO getUpdateDto(MsWpToolBasicDTO msWpToolBasicDTO) throws ParseException {
        MsWpToolBasicDTO oldDTO = msWpToolBasicDao.findMsWpToolBasicDTOById(msWpToolBasicDTO.getMsWpToolId());
        if(oldDTO == null){
            throw new DaoException("数据不存在");
        }
        PojoUtil.copyProperties(oldDTO,msWpToolBasicDTO,true);
        oldDTO.setLastUpdateDate(DateUtil.nowTime());
        try{
            InetAddress ip4 = Inet4Address.getLocalHost();
            oldDTO.setLastUpdateIp(ip4.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return oldDTO;
    }

    /**
     * 根据主键删除数据
     * @param id
     * @return
     */
    @Transactional
    public int deleteByMsWpToolBasicDTOById(String id) {
        if (StringUtil.isEmpty(id)) {
            throw new DaoException("删除失败，参数为null");
        }
        try {
            MsWpToolBasicDTO msWpToolBasicDTO = msWpToolBasicDao.findMsWpToolBasicDTOById(id);
            if (msWpToolBasicDTO == null) {
                throw new DaoException(("删除失败，数据为空"));
            }
            int count = msWpToolBasicDao.deleteByMsWpToolBasicDTOById(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
}
